package com.ruoyi.service.traceability.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.traceability.MesTraceabilityRecordMapper;
import com.ruoyi.domain.traceability.MesTraceabilityRecord;
import com.ruoyi.service.traceability.IMesTraceabilityRecordService;

/**
 * 追溯记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class MesTraceabilityRecordServiceImpl implements IMesTraceabilityRecordService {
    @Autowired
    private MesTraceabilityRecordMapper mesTraceabilityRecordMapper;

    /**
     * 查询追溯记录
     * 
     * @param traceId 追溯记录主键
     * @return 追溯记录
     */
    @Override
    public MesTraceabilityRecord selectMesTraceabilityRecordByTraceId(Long traceId) {
        return mesTraceabilityRecordMapper.selectMesTraceabilityRecordByTraceId(traceId);
    }

    /**
     * 查询追溯记录列表
     * 
     * @param mesTraceabilityRecord 追溯记录
     * @return 追溯记录
     */
    @Override
    public List<MesTraceabilityRecord> selectMesTraceabilityRecordList(MesTraceabilityRecord mesTraceabilityRecord) {
        return mesTraceabilityRecordMapper.selectMesTraceabilityRecordList(mesTraceabilityRecord);
    }

    /**
     * 新增追溯记录
     * 
     * @param mesTraceabilityRecord 追溯记录
     * @return 结果
     */
    @Override
    public int insertMesTraceabilityRecord(MesTraceabilityRecord mesTraceabilityRecord) {
        mesTraceabilityRecord.setCreateTime(DateUtils.getNowDate());
        return mesTraceabilityRecordMapper.insertMesTraceabilityRecord(mesTraceabilityRecord);
    }

    /**
     * 修改追溯记录
     * 
     * @param mesTraceabilityRecord 追溯记录
     * @return 结果
     */
    @Override
    public int updateMesTraceabilityRecord(MesTraceabilityRecord mesTraceabilityRecord) {
        mesTraceabilityRecord.setUpdateTime(DateUtils.getNowDate());
        return mesTraceabilityRecordMapper.updateMesTraceabilityRecord(mesTraceabilityRecord);
    }

    /**
     * 批量删除追溯记录
     * 
     * @param traceIds 需要删除的追溯记录主键
     * @return 结果
     */
    @Override
    public int deleteMesTraceabilityRecordByTraceIds(Long[] traceIds) {
        return mesTraceabilityRecordMapper.deleteMesTraceabilityRecordByTraceIds(traceIds);
    }

    /**
     * 删除追溯记录信息
     * 
     * @param traceId 追溯记录主键
     * @return 结果
     */
    @Override
    public int deleteMesTraceabilityRecordByTraceId(Long traceId) {
        return mesTraceabilityRecordMapper.deleteMesTraceabilityRecordByTraceId(traceId);
    }

    /**
     * 根据工单ID查询追溯记录
     * 
     * @param workOrderId 工单ID
     * @return 追溯记录列表
     */
    @Override
    public List<MesTraceabilityRecord> selectTraceabilityByWorkOrderId(Long workOrderId) {
        return mesTraceabilityRecordMapper.selectTraceabilityByWorkOrderId(workOrderId);
    }

    /**
     * 根据批次号查询追溯记录
     * 
     * @param batchCode 批次号
     * @return 追溯记录列表
     */
    @Override
    public List<MesTraceabilityRecord> selectTraceabilityByBatchCode(String batchCode) {
        return mesTraceabilityRecordMapper.selectTraceabilityByBatchCode(batchCode);
    }

    /**
     * 根据产品ID查询追溯记录
     * 
     * @param productId 产品ID
     * @return 追溯记录列表
     */
    @Override
    public List<MesTraceabilityRecord> selectTraceabilityByProductId(Long productId) {
        return mesTraceabilityRecordMapper.selectTraceabilityByProductId(productId);
    }

    /**
     * 根据追溯类型查询追溯记录
     * 
     * @param traceType 追溯类型
     * @return 追溯记录列表
     */
    @Override
    public List<MesTraceabilityRecord> selectTraceabilityByType(String traceType) {
        return mesTraceabilityRecordMapper.selectTraceabilityByType(traceType);
    }

    /**
     * 校验追溯编码是否唯一
     * 
     * @param traceCode 追溯编码
     * @return 结果
     */
    @Override
    public String checkTraceCodeUnique(String traceCode) {
        MesTraceabilityRecord record = mesTraceabilityRecordMapper.checkTraceCodeUnique(traceCode);
        if (record != null) {
            return "1"; // 不唯一
        }
        return "0"; // 唯一
    }

    /**
     * 批量插入追溯记录
     * 
     * @param traceabilityRecords 追溯记录列表
     * @return 结果
     */
    @Override
    public int batchInsertTraceabilityRecord(List<MesTraceabilityRecord> traceabilityRecords) {
        return mesTraceabilityRecordMapper.batchInsertTraceabilityRecord(traceabilityRecords);
    }
}